import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:thingskit/Pages/DeviceSetting/deviceSettingPage.dart';
import 'package:thingskit/Pages/My/MyPage.dart';
import 'package:thingskit/apiService/device.dart';
import 'package:thingskit/components/titleCard.dart';
import '../../components/basicCard.dart';
import '../../components/optionButton.dart';
import '../DeviceGroup/DeviceGroupPage.dart';
import '../ModeManagement/modeManagementPage.dart';

class HomeController extends GetxController {
  final DeviceApi deviceApi = DeviceApi();
  RxList<String> deviceValues = <String>[].obs;
  List<String> deviceLabels = ['设备数', '在线', '离线'];
  List<String> energyValues =['10度', '100%', '100小时'];
  List<String> energyLabels = ['用电量', '节能率', '运行时间'];

  @override
  void onInit() {
    super.onInit();
    // 获取设备列表
    deviceApi.getDeviceList().then((value) {
      if (value != null) {
        deviceValues.value = [
          value['device_count'].toString(),
          value['online_count'].toString(),
          value['offline_count'].toString(),
        ];
      }
    });
  }
}

class HomePage extends StatelessWidget {
  final HomeController controller = Get.put(HomeController());

  HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return  SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Column(
                children: [
                  TitleCard(title: '基础统计'),
                  SizedBox(height: 20),
                  Obx(()=>BasicCard(
                    icon: Icons.ballot_outlined,
                    title: '设备统计',
                    values: controller.deviceValues.toList(),
                    labels: controller.deviceLabels,
                  ),),
                  SizedBox(height: 20),
                  BasicCard(
                    icon: Icons.energy_savings_leaf,
                    title: '能耗统计',
                    values: controller.energyValues,
                    labels: controller.energyLabels,
                  ),
                  SizedBox(height: 16),
                ],
              ),
              Column(
                children: [
                  TitleCard(title: '现场控制'),
                  SizedBox(height: 16),
                  Wrap(
                    spacing: 8,
                    runSpacing: 8,
                    children: [
                      OptionButton(
                        icon: Icons.groups,
                        title: '设备分组',
                        onPressed: () => Get.to(() => DeviceGroupPage()),
                      ),
                      OptionButton(
                        icon: Icons.settings,
                        title: '设备配置',
                        onPressed: () => Get.to(() => DeviceSettingPage()),
                      ),
                      OptionButton(
                        icon: Icons.schema_outlined,
                        title: '方案管理',
                        onPressed: () => Get.to(() => ModeManagementPage()),
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ),
        ),
    );
  }
}
